﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace TurtleEngine
{
	 /// <summary>
	 ///
	 /// </summary>
	 public class Utility
	 {
		  /// <summary>
		  /// Gets or sets the name of the database.
		  /// </summary>
		  /// <value>
		  /// The name of the database.
		  /// </value>
		  public static string DatabaseName { get; set; }

		  /// <summary>
		  /// Gets or sets the database server.
		  /// </summary>
		  /// <value>
		  /// The database server.
		  /// </value>
		  public static string DatabaseServer { get; set; }

		  /// <summary>
		  /// Gets or sets the DB connection.
		  /// </summary>
		  /// <value>
		  /// The DB connection.
		  /// </value>
		  public static SqlConnection DBConnection { get; set; }

		  /// <summary>
		  /// Prints the datatable.
		  /// </summary>
		  /// <param name="dt">The dt.</param>
		  public static void PrintDatatable(DataTable dt)
		  {
				TextWriter tw = new StreamWriter("datatable.txt");

				foreach (DataColumn column in dt.Columns)
					 tw.Write(column + "\t");

				tw.WriteLine();
				foreach (DataRow row in dt.Rows)
				{
					 foreach (DataColumn column in dt.Columns)
					 {
						  tw.Write(row[column] + "\t");
					 }
					 tw.WriteLine();
				}
				tw.Close();
		  }

		  /// <summary>
		  /// Splits the string.
		  /// </summary>
		  /// <param name="input">The input.</param>
		  /// <param name="splitLength">Length of the split.</param>
		  /// <returns></returns>
		  public static string SplitString(string input, int splitLength)
		  {
				int fullLength = input.Length;
				string piece = string.Empty;
				var lineArray = new ArrayList();
				while (fullLength > splitLength)
				{
					 piece = input.Substring(0, splitLength);
					 input = input.Substring(splitLength);
					 lineArray.Add(piece);
					 fullLength = input.Length;
				}
				lineArray.Add(input);
				return string.Join(Environment.NewLine, (string[])lineArray.ToArray(typeof(string)));
		  }

		  /// <summary>
		  /// Writes the HTML.
		  /// </summary>
		  /// <param name="xmlfile">The xmlfile.</param>
		  /// <param name="xslFile">The XSL file.</param>
		  /// <param name="htmlFile">The HTML file.</param>
		  public static void WriteHTML(string xmlfile, string xslFile, string htmlFile)
		  {
				var ht = new HTMLTransfomer(xmlfile, xslFile, htmlFile);
				ht.TableTransformer();
		  }

		  /// <summary>
		  /// Writes the XML.
		  /// </summary>
		  /// <param name="dt">The dt.</param>
		  /// <param name="xmlfile">The xmlfile.</param>
		  public static void WriteXML(DataTable dt, string xmlfile)
		  {
				var ds = new DataSet(dt.TableName);
				ds.Tables.Add(dt.Copy());
				File.WriteAllText(@"xml\" + xmlfile, ds.GetXml());
		  }
	 }
}